package com.ymx.darling.generater;

import com.ymx.darling.exception.DarlingException;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

public class TemplateContent {
    private static final Map<String, String> content = new HashMap<>();
    static {
        // 读取文件内容
        content.put("entity", readContent("templates/entity.txt"));
        content.put("mapper", readContent("templates/mapper.txt"));
        content.put("service", readContent("templates/service.txt"));
        content.put("service-mpl", readContent("templates/service-mpl.txt"));
        content.put("spring-service", readContent("templates/spring-service.txt"));
        content.put("spring-service-mpl", readContent("templates/spring-service-mpl.txt"));
    }

    private static String readContent(String filePath) {
        try {
            InputStream resource = Thread.currentThread().getContextClassLoader()
                .getResourceAsStream(filePath);
            byte[] bytes = new byte[1512];
            int count = 0;
            if (resource != null) {
                count = resource.read(bytes);
            }
            if (resource != null) {
                resource.close();
            }
            return new String(bytes, 0, count);
        } catch (Exception e) {
            throw new DarlingException("读取文件"+filePath+"出错");
        }
    }


    public static String getContent(String contentKey) {
        return content.get(contentKey);
    }
}
